﻿using System;
using System.ComponentModel.DataAnnotations;
using FreeSql;
using EAF.Utility;
using EAF.DBC;
using App.Utility;
using App.Model.Main;

namespace App.DTO.Framework
{
    /// <summary>
    /// 类型测试 业务字段
    /// </summary>
    public class TypeTestBaseDtoDemo
    {
        /// <summary>
        /// 编号
        /// </summary> 
        [Display(Name = "编号")]
        public virtual int Id { get; set; }

        /// <summary>
        /// 整型
        /// </summary>
        [Display(Name = "整型")]
        public virtual int Int { get; set; }
        /// <summary>
        /// 普通输入框 必填
        /// </summary>
        [Display(Name = "普通输入框")]
        //[NotNull]
        [StrMaxLen(10, ErrorMessage = "普通输入框自定义 不能超过10个字符")]
        public virtual string NvarcharInput { get; set; }
        /// <summary>
        /// 输入框大于200，小于2000 必填
        /// </summary>
        [Display(Name = "输入框大于200")]
        [NotNull]
        [StrMaxLen(2000)]
        public virtual string NvarcharTextArea { get; set; }
        /// <summary>
        /// 布尔类型
        /// </summary>
        [Display(Name = "布尔类型")]
        public virtual bool Bit { get; set; }
        /// <summary>
        /// 时间控件类型
        /// </summary>
        [Display(Name = "时间控件类型")]
        public virtual DateTime DateTime { get; set; }
        /// <summary>
        /// 保留2位小数
        /// </summary>
        [Display(Name = "保留2位小数")]
        public virtual decimal Decimal { get; set; }
        /// <summary>
        /// 浮点类型
        /// </summary>
        [Display(Name = "浮点类型")]
        public virtual float Float { get; set; }
        /// <summary>
        /// 浮点类型
        /// </summary>
        [Display(Name = "浮点类型")]
        public virtual double Double { get; set; }
        /// <summary>
        /// 富文本类型 必填
        /// </summary>
        [NotNull]
        [Display(Name = "富文本类型")]
        public virtual string Text { get; set; }
        /// <summary>
        /// 数据版本 业务中无需显示维护值 用于乐观锁添加传0 更新传详情接口的值
        /// </summary>
        [Display(Name = "数据版本")]
        public virtual int Version { get; set; } = 0;
    }

    /// <summary>
    /// 类型测试 分页请求json实体类 这里字段请使用首字母小写
    /// </summary>
    public partial class TypeTestPageReqDemo : DtoPageReqBase
    {
        /// <summary>
        /// 常用字段搜索 默认根据支持CreaterName模糊查询
        /// </summary>
        [Display(Name = "常用字段搜索")]
        public string searchValue { get; set; }

        /// <summary>
        /// 查询参数处理
        /// </summary>
        /// <param name="dbc">表所在库上下文</param>
        /// <returns></returns>
        public ISelect<TypeTest> Query(BaseDbContext dbc)
        {
            var query = dbc.Select<TypeTest>();
            //显示模式处理
            if (showMode != ShowMode.Normal)
            {
                query.DisableGlobalFilter();
                if (showMode == ShowMode.Deleted)
                {
                    query = query.Where(w => w.Deleted == true);
                }
            }

            //前端不传入whereDynamicFilter高级过滤 特殊条件过滤
            if (whereDynamicFilter.IsNull())
            {
                //这里处理常用字段查询处理
                query = query.WhereIf(searchValue.HasValue(), w => w.CreaterName.Contains(searchValue));
            }
            else
            {
                //高级过滤查询
                var whereObj = dbc.ParseWhereDynamicFilter(whereDynamicFilter);
                if (whereObj != null)
                {
                    query = query.WhereDynamicFilter(whereObj);
                }
            }
            //前端不传排序字符串 则按id降序
            if (orderBy == null)
            {
                query = query.OrderByDescending(f => f.Id);
            }
            else
            {
                query = query.OrderBy(orderBy);
            }

            return query;
        }

    }

    /// <summary>
    /// 类型测试 请求json实体类
    /// </summary>
    public partial class TypeTestReqDemo : TypeTestBaseDtoDemo
    {
        /// <summary>
        /// json实例转换为db实例 就是将同名字段赋值到dbEntity 相当于更新
        /// </summary>
        /// <param name="dbEntity">表实例</param>
        /// <returns></returns>
        public TypeTest ConvertDbEntity(TypeTest dbEntity)
        {
            this.AutoMapObjectTo(dbEntity);

            //无法自动绑定的在此手动绑定


            return dbEntity;
        }

        #region 通用字段

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        public virtual string Description { get; set; }
        /// <summary>
        /// 排序码
        /// </summary>
        [Display(Name = "排序码")]
        public virtual int? SortNo { get; set; }
        /// <summary>
        /// 是否有效
        /// </summary>
        [Display(Name = "是否有效")]
        public virtual bool Valid { get; set; }

        #endregion

    }

    /// <summary>
    /// 类型测试 响应json实体类
    /// </summary>
    public partial class TypeTestRespDemo : TypeTestBaseDtoDemo
    {
        public TypeTestRespDemo() { }
        /// <summary>
        /// db实例转换json实例 就是将同名字段赋值到当前实例 相当于更新复制
        /// </summary>
        /// <param name="dbEntity">db实例</param>
        public TypeTestRespDemo(TypeTest dbEntity)
        {
            dbEntity.AutoMapObjectTo(this);

            //无法自动绑定的在此手动绑定

        }

        #region 通用字段

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        public virtual string Description { get; set; }
        /// <summary>
        /// 排序数字
        /// </summary>
        [Display(Name = "排序数字")]
        public virtual int? SortNo { get; set; }
        /// <summary>
        /// 是否有效
        /// </summary>
        [Display(Name = "是否有效")]
        public virtual bool Valid { get; set; }
        /// <summary>
        /// 创建者ID
        /// </summary>
        [Display(Name = "创建者ID")]
        public virtual int? CreaterId { get; set; }
        /// <summary>
        /// 创建人名
        /// </summary>
        [Display(Name = "创建人名")]
        public virtual string CreaterName { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [Display(Name = "创建时间")]
        public virtual DateTime CreateTime { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        [Display(Name = "修改时间")]
        public virtual DateTime? UpdateTime { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        [Display(Name = "是否删除")]
        public virtual bool Deleted { get; set; }
        /// <summary>
        /// 删除时间
        /// </summary>
        [Display(Name = "删除时间")]
        public virtual DateTime? DeleteTime { get; set; }

        #endregion

    }

}
